home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / PCI Driver Development Kit / • Tools / Utility / DisplayNameRegistry 950412 / Src / SaveNameRegistry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-27  |  6.3 KB  |  291 lines  |  [TEXT/MPCC]

  1. /*                                AuditFileDialog.c                                */
  2. /*
  3.  * AuditFileDialog.c
  4.  * Copyright © 1995, Apple Computer Inc. All Rights Reserved.
  5.  */
  6. #include "DisplayNameRegistry.h"
  7.  
  8. #define kEndOfLine                0x0D    /* Return            */
  9. #define TestTDFlag(elementHdl, mask)    (((**elementHdl).flag & (mask)) != 0)
  10.  
  11. /*
  12.  * Standard File dialog items. This follows the layout of the old Standard File
  13.  * (-3999) DITL. Our private items are at the end.
  14.  */
  15. enum {                                        /* Save File Dialog                */
  16.     kSaveButton = 1,
  17.     kDontSaveButton,
  18.     kSaveAsButton,
  19.     kFolderHierarchyPopup,
  20.     kEjectButton,
  21.     kDriveButton,
  22.     kFileNameText,
  23.     kCatalogList,
  24.     kSaveAllRadio,
  25.     kSaveVisibleRadio
  26. };
  27.  
  28. OSErr                        WriteVisibleNameRegistry(
  29.         TwistDownHdl            twistDownHandle
  30.     );
  31. OSErr                        WriteEntireNameRegistry(
  32.         TwistDownHdl            twistDownHandle
  33.     );
  34. OSErr                        WriteThisElement(
  35.         TwistDownHdl            twistDownHandle
  36.     );
  37. void                        SaveFileDialog(
  38.         short                    dialogID,
  39.         ConstStr255Param        promptString,
  40.         ConstStr255Param        originalName,
  41.         SFReply                    *reply
  42.     );
  43. static pascal short            PutDialogFilter(
  44.         short                    itemHit,
  45.         DialogPtr                theDialog
  46.     );
  47. static void                    SelectDialogRadio(
  48.         DialogPtr                theDialog,
  49.         short                    selectedButton
  50.     );
  51. void                        CheckForNullByte(        /* Debug only */
  52.         Ptr                        dataPtr,
  53.         long                    dataLength
  54.     );
  55.  
  56. /*
  57.  * Create an output file. .
  58.  */
  59. void
  60. CreateOutputFile(void)
  61. {
  62.         OSErr                    status;
  63.         OSErr                    closeStatus;
  64.         TwistDownHdl            listHead;
  65.         Cell                    theCell;
  66.         
  67.         SaveFileDialog(
  68.             DLOG_SFPutFile,
  69.             "\pOutput File",
  70.             "\pName Registry Output",
  71.             &gSFReply
  72.         );
  73.         if (gSFReply.good == FALSE)
  74.             status = userCanceledErr;
  75.         else {
  76.             /*
  77.              * Create the file, elmininating any duplicate.
  78.              */
  79.             SetCursor(*GetCursor(watchCursor));
  80.             status = Create(gSFReply.fName, gSFReply.vRefNum, 'TTXT', 'TEXT');
  81.             if (status == dupFNErr)     {                    /* Exists already?        */
  82.                 status = FSDelete(gSFReply.fName, gSFReply.vRefNum);
  83.                 if (status == noErr) {
  84.                     status = Create(
  85.                         gSFReply.fName, gSFReply.vRefNum, 'TTXT', 'TEXT');
  86.                 }
  87.             }
  88.             if (status == noErr) {
  89.                 status = FSOpen(
  90.                     gSFReply.fName, gSFReply.vRefNum, &gSaveFileRefNum);
  91.             }
  92.             if (status != noErr) {
  93.                 if (status != userCanceledErr)
  94.                     NonFatalError(status, "\pCan't create file");
  95.             }
  96.             else {
  97.                 SetPt(&theCell, 0, 0);
  98.                 listHead = GetTwistDownElementHandle(
  99.                             gCurrentBrowserPtr->theList, theCell);
  100.                 if (gSaveAllElements)
  101.                     status = WriteEntireNameRegistry(listHead);
  102.                 else {
  103.                     status = WriteVisibleNameRegistry(listHead);
  104.                 }
  105.                 if (status != noErr)
  106.                     NonFatalError(status, "\pCan't write file");
  107.                 closeStatus = FSClose(gSaveFileRefNum);
  108.                 if (status == noErr && closeStatus != noErr) {
  109.                     NonFatalError(closeStatus, "\pCan't close file");
  110.                     status = closeStatus;
  111.                 }
  112.                 closeStatus = FlushVol(NULL, gSFReply.vRefNum);
  113.                 if (status == noErr && closeStatus != noErr)
  114.                     NonFatalError(closeStatus, "\pCan't flush volume");
  115.             }
  116.             InitCursor();
  117.         }
  118. }
  119.  
  120. OSErr
  121. WriteVisibleNameRegistry(
  122.         TwistDownHdl            twistDownHandle
  123.     )
  124. {
  125.         OSErr                    status;
  126.  
  127.         status = noErr;
  128.         while (status == noErr && twistDownHandle != NULL) {
  129.             status = WriteThisElement(twistDownHandle);
  130.             if (status == noErr
  131.              && (**twistDownHandle).subElement != NULL
  132.              && TestTDFlag(twistDownHandle, kShowSublist))
  133.                 status = WriteVisibleNameRegistry((**twistDownHandle).subElement);
  134.             twistDownHandle = (**twistDownHandle).nextElement;
  135.         }
  136.         return (status);
  137. }
  138.  
  139. OSErr
  140. WriteEntireNameRegistry(
  141.         TwistDownHdl                twistDownHandle
  142.     )
  143. {
  144.         OSErr                        status;
  145.         
  146.         status = noErr;
  147.         while (status == noErr && twistDownHandle != NULL) {
  148.             status = WriteThisElement(twistDownHandle);
  149.             if (status == noErr && (**twistDownHandle).subElement != NULL)
  150.                 status = WriteEntireNameRegistry((**twistDownHandle).subElement);
  151.             twistDownHandle = (**twistDownHandle).nextElement;
  152.         }
  153.         return (status);
  154. }
  155.  
  156. /*
  157.  * Write one line of text to the output file.
  158.  */
  159. OSErr
  160. WriteThisElement(
  161.         TwistDownHdl                twistDownHdl
  162.     )
  163. {
  164.         OSErr                        status;
  165.         long                        textLength;
  166.         short                        handleState;
  167.         static char                    gEndOfLine[1] = { kEndOfLine };
  168. #define ELEM (**twistDownHdl)
  169.  
  170.         handleState = HGetState((Handle) twistDownHdl);
  171.         HLock((Handle) twistDownHdl);
  172.         textLength = ELEM.dataLength;
  173.         if (textLength == 0)
  174.             status = noErr;
  175.         else {
  176.             CheckForNullByte((Ptr) ELEM.data, ELEM.dataLength);
  177.             status = FSWrite(gSaveFileRefNum, &textLength, ELEM.data);
  178.         }
  179.         HSetState((Handle) twistDownHdl, handleState);
  180.         if (status == noErr) {
  181.             textLength = 1;
  182.             status = FSWrite(gSaveFileRefNum, &textLength, gEndOfLine);
  183.         }
  184.         if (status != noErr)
  185.             NonFatalError(status, "\pWriteThisElement");
  186.         return (status);
  187. }
  188.  
  189. void
  190. SaveFileDialog(
  191.         short                    dialogID,
  192.         ConstStr255Param        promptString,
  193.         ConstStr255Param        originalName,
  194.         SFReply                    *reply
  195.     )
  196. {
  197.         static DlgHookUPP        dlgHookUPP = NULL;
  198.         Point                    where;
  199.  
  200.         SetPt(&where, 80, 80);
  201.         if (dlgHookUPP == NULL)
  202.             dlgHookUPP = NewDlgHookProc(PutDialogFilter);
  203.         SFPPutFile(
  204.             where,
  205.             promptString,
  206.             originalName,
  207.             dlgHookUPP,
  208.             reply,
  209.             dialogID,
  210.             NULL                                /* No Modal Dialog Filter        */
  211.         );
  212. }
  213.  
  214. static pascal short
  215. PutDialogFilter(
  216.         short                    itemHit,
  217.         DialogPtr                theDialog
  218.     )
  219. {
  220.         short                    itemType;
  221.         Handle                    itemHandle;
  222.         Rect                    itemRect;
  223.  
  224.         switch (itemHit) {
  225.         case sfHookFirstCall:
  226.             SelectDialogRadio(
  227.                 theDialog,
  228.                 (gSaveAllElements) ? kSaveAllRadio : kSaveVisibleRadio
  229.             );
  230.             break;
  231.         case kSaveAllRadio:
  232.         case kSaveVisibleRadio:
  233.             SelectDialogRadio(theDialog, itemHit);
  234.             break;
  235.         case kSaveButton:
  236.             GetDialogItem(
  237.                 theDialog,
  238.                 kSaveAllRadio,
  239.                 &itemType,
  240.                 &itemHandle,
  241.                 &itemRect
  242.             );
  243.             gSaveAllElements = GetControlValue((ControlHandle) itemHandle);
  244.         }
  245.         return (itemHit);
  246. }        
  247.  
  248.  
  249. static void
  250. SelectDialogRadio(
  251.         DialogPtr                theDialog,
  252.         short                    selectedButton
  253.     )
  254. {
  255.         short                    itemType;
  256.         Handle                    itemHandle;
  257.         Rect                    itemRect;
  258.         short                    i;
  259.  
  260.         for (i = kSaveAllRadio; i <= kSaveVisibleRadio; i++) {
  261.             GetDialogItem(
  262.                 theDialog,
  263.                 i,
  264.                 &itemType,
  265.                 &itemHandle,
  266.                 &itemRect
  267.             );
  268.             SetControlValue(
  269.                 (ControlHandle) itemHandle,
  270.                 (i == selectedButton) ? 1 : 0
  271.             );
  272.         }
  273. }
  274.  
  275. void
  276. CheckForNullByte(
  277.         Ptr                        dataPtr,
  278.         long                    dataLength
  279.     )
  280. {
  281.         long                    i;
  282.         
  283.         for (i = 0; i < dataLength; i++) {
  284.             if (dataPtr[i] == 0) {
  285.                 printf("%ld in %ld \"%.*s\"\n", i, dataLength, dataLength, dataPtr);
  286.                 break;
  287.             }
  288.         }
  289. }
  290.  
  291.